package com.np.np_android;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.np.np_android.model.OrderSheet;
import com.np.np_android.model.Product;

import java.util.List;

/***
 *
 */
public class SimpleAdapter extends BaseRecyclerAdapter<SimpleAdapter.SimpleAdapterViewHolder> implements View.OnClickListener {
    private List<Product> list;
    private int largeCardHeight, smallCardHeight;
    private MyItemClickListener mListener;
    int POSITION;
    private  Context context;


    public SimpleAdapter(List<Product> list, Context context,MyItemClickListener listener) {
        this.list = list;
        mListener = listener;
        largeCardHeight = DensityUtil.dip2px(context, 150);
        smallCardHeight = DensityUtil.dip2px(context, 100);

        this.context = context;
    }
//    public void setData(List<Product> list){
//
//    }

    @Override
    public void onBindViewHolder(SimpleAdapterViewHolder holder, int position, boolean isItem) {
        Product product= list.get(position);
        holder.name.setText(product.getName());
        holder.store.setText(""+product.getStore());
        holder.price.setText(""+product.getPrice());
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            holder.rootView.getLayoutParams().height = position % 2 != 0 ? largeCardHeight : smallCardHeight;
        }

        POSITION = position;

        holder.itemView.setTag(position);
    }




    public int getPosition() {
        return POSITION;
    }

    /**
     * 点击监听
     */
    public void onClick(View v) {
        if(mListener != null){
            mListener.onItemClick(v, (int) v.getTag());
        }
    }


    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }

    public void setData(List<Product> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_recylerview, parent, false);
        SimpleAdapterViewHolder vh = new SimpleAdapterViewHolder(v, true);
        v.setOnClickListener(this);
        return vh;
    }

    public void insert(Product person, int position) {
        insert(list, person, position);
    }

    public void remove(int position) {
        remove(list, position);
    }

    public void clear() {
        clear(list);
    }

    public class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {

        public View rootView;
        public TextView name;
        public TextView price;
        public TextView store;
        public int position;

        public SimpleAdapterViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                name = (TextView) itemView
                        .findViewById(R.id.recycler_view_test_item_name);
                price = (TextView) itemView
                        .findViewById(R.id.recycler_view_test_item_price);
                store = (TextView) itemView
                        .findViewById(R.id.recycler_view_test_item_store);
                rootView = itemView
                        .findViewById(R.id.card_view);
            }

        }
    }

    public Product getItem(int position) {
        if (position < list.size())
            return list.get(position);
        else
            return null;
    }


    public void setOnItemClickListener(MyItemClickListener listener) {
        this.mListener = listener;
    }

}